package com.keyancompany.zhongbaopingtai.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.activity.ComplaintDetailActivity;
import com.keyancompany.zhongbaopingtai.activity.ProjectDetailActivity;
import com.keyancompany.zhongbaopingtai.adapter.ComplainingAdapter;
import com.keyancompany.zhongbaopingtai.adapter.FindAdapter;
import com.keyancompany.zhongbaopingtai.api.CollectionAPI;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.api.UserAPI;
import com.keyancompany.zhongbaopingtai.listener.IAsyncHttpResponseHandler;
import com.keyancompany.zhongbaopingtai.listener.IClickCancleListener;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.ComplainingModel;
import com.keyancompany.zhongbaopingtai.model.FindModel;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.weight.IListView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnItemClick;

/**
 * 我的申诉
 * <p>
 * Created by firefox on 2016/6/15.
 */
public class ComplainingFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    public static final String ARG_PAGE = "ARG_PAGE";
    @Bind(R.id.list_view)
    IListView listView;
    @Bind(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;
    @Bind(R.id.empty_hint)
    TextView emptyHint;
    @Bind(R.id.empty_view)
    LinearLayout emptyView;

    ComplainingAdapter mAdapter;

    private int currentPage = 1, total = 1;

    public static ComplainingFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        ComplainingFragment pageFragment = new ComplainingFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_complaining;
    }

    @Override
    public void setUpViews() {
        initSwipeLayout(swipeLayout);
        mAdapter = new ComplainingAdapter(getActivity(), null);
        listView.setAdapter(mAdapter);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.post(new Thread(new Runnable() {

            @Override
            public void run() {
                swipeLayout.setRefreshing(true);
                onRefresh();
            }
        }));
        listView.setStateListener(new IListView.SwipeLayoutState() {
            @Override
            public boolean isRefresh() {
                return swipeLayout.isRefreshing();
            }

            @Override
            public boolean isLastPage() {
                return total == currentPage;
            }

            @Override
            public void requestNextPage() {
                requestTask(++currentPage);
            }
        });

    }

    @OnItemClick(R.id.list_view)
    public void listItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position < mAdapter.getCount()) {
//            IntentUtils.startActivity(context, ChatActivity.class, Constants.CHAT_DETIAL, messageAdapter.getItem(position));
            IntentUtils.startActivity(context, ComplaintDetailActivity.class, Constants.ITEM, mAdapter.getItem(position));
        }
    }

    public void requestTask(int cPage) {

        /**
         * 申述
         */
        UserAPI.complaining("" + cPage, new IAsyncHttpResponseHandler() {
            @Override
            public void start(int type) {

            }

            @Override
            public void success(int type, String json) {
                swipeLayout.setRefreshing(false);
//        Log.e("FindFragment", " ---- " + json);
                if (currentPage == 1) {
                    mAdapter.clear();
                }

                if (type == UserAPI.DISPUTES_LIST) {
                    try {
                        BaseModel base = JSONObject.parseObject(json, BaseModel.class);
                        Log.e("FindFragment", " ---- " + base.toString());
                        if (base.isSuccess()) {
                            JSONObject object = JSONObject.parseObject(base.data);
                            currentPage = object.getInteger(Constants.CURRENT);
                            total = object.getInteger(Constants.TOTAL);
                            List<ComplainingModel> list = JSONObject.parseArray(object.getString("list"), ComplainingModel.class);
                            if (mAdapter == null || list.isEmpty()) return;
                            listView.loadMoreFinish();
                            if (total == currentPage) {
                                listView.removeFooterView();
                            }
                            mAdapter.addAllAfter(list);
                            mAdapter.notifyDataSetChanged();
                        }
                    } catch (Exception e) {
                        Log.e("FindFragment", " ---- " + e.toString());
                    }
                }

            }

            @Override
            public void failed(String error) {
                swipeLayout.setRefreshing(false);
            }

            @Override
            public void finish(int type) {

            }
        }, null);

    }

    @Override
    public void onRefresh() {

        currentPage = 1;
        total = 1;
        requestTask(currentPage);
    }
}
